package activity.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.hontech.pizzav2.R
import mvc.model.inventory.ScanModel


class ScanItem(view: View) : RecyclerView.ViewHolder(view) {

    private val mTextGoodsType = view.findViewById<TextView>(R.id.id_scan_item_text_goods_type)
    private val mTextBarCode = view.findViewById<TextView>(R.id.id_scan_item_text_bar_code)
    private val mTextStatus = view.findViewById<TextView>(R.id.id_scan_item_text_status)

    fun setUp(model: ScanModel) {
        mTextGoodsType.text = model.name()
        mTextBarCode.text = model.barCode
        if (model.productionDate == -1L) {
            mTextStatus.text = "Barcode format error"
        } else {
            mTextStatus.text = model.result
        }
    }

}

class ScanAdapter(val models: ArrayList<ScanModel>) : RecyclerView.Adapter<ScanItem>() {

    override fun onBindViewHolder(item: ScanItem, position: Int) {
        item.setUp(models[position])
    }

    override fun getItemCount(): Int {
        return models.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): ScanItem {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_scan, parent, false)
        return ScanItem(view)
    }
}
